﻿using System;
using log4net;
using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.MatrixCloudApi;
using Matrix.MaKaYunShang.Model.MatrixCloudApi;

namespace Matrix.MaKaYunShang.WeChat.CardProccess
{
    public class WechatCardEntries
    {
        private readonly ILog _fileLog = LogManager.GetLogger(typeof(WechatCardEntries));

        private readonly MxApiWechatInfo _wechatInfo;
        private readonly MxApiWechatEventHistory _wechatEventHistory;
        private WeChatCardEntry _weChatCardEntry;

        public WechatCardEntries(MxApiWechatEventHistory eventHistory, MxApiWechatInfo wechatInfo)
        {
            _wechatInfo = wechatInfo;
            _wechatEventHistory = eventHistory;
            if (_wechatInfo == null || _wechatInfo.Id < 1 || _wechatEventHistory == null ||
                string.IsNullOrEmpty(_wechatEventHistory.Event) || string.IsNullOrEmpty(_wechatEventHistory.CardId))
            {
                _fileLog.Error("WechatCardEntries | WechatCardEntries | Error event info or wechat info");
                return;
            }
            _weChatCardEntry = WeChatCardEntryBLL.Instance.GetModelByCardId(_wechatEventHistory.CardId);

        }

        /// <summary>
        /// 卡券事件处理
        /// </summary>
        public void CardEventProccess()
        {
            if (_wechatInfo == null || _wechatInfo.Id == 0 || _wechatEventHistory == null ||
                string.IsNullOrWhiteSpace(_wechatEventHistory.Event) ||
                string.IsNullOrWhiteSpace(_wechatEventHistory.CardId) || _weChatCardEntry == null ||
                _weChatCardEntry.Id == 0 || string.IsNullOrWhiteSpace(_weChatCardEntry.CardId))
            {
                _fileLog.Error("WechatCardEntry | CardProccess | Error event info or wechat info ");
                return;
            }

#if DEBUG
            _fileLog.Info("WechatCardEntry | CardProccess Begin - " + _wechatEventHistory.Event);
#endif

            switch (_wechatEventHistory.Event.ToLower())
            {
                case "card_pass_check"://卡券通过审核
                    CardAuditEvent(_weChatCardEntry, _wechatEventHistory, WechatCommonCardStatus.CARD_STATUS_VERIFY_OK);
                    break;
                case "card_not_pass_check"://卡券未通过审核
                    CardAuditEvent(_weChatCardEntry, _wechatEventHistory, WechatCommonCardStatus.CARD_STATUS_VERIFY_FAIL);
                    break;
                case "user_get_card"://用户_weChatCardEntry领取卡券
                    CustomerGetCardEvent(_weChatCardEntry, _wechatEventHistory);
                    break;
                case "user_del_card"://用户删除卡券
                    UserDeleteCardEvent(_weChatCardEntry, _wechatEventHistory);
                    break;
                case "user_view_card"://进入会员卡事件推送
                    break;

                case "user_consume_card"://核销事件
                    break;
                case "User_pay_from_pay_cell"://微信买单事件
                    break;
                case "user_enter_session_from_card"://用户从卡券进入公众号会话
                    break;
            }
        }

        #region 卡卷事件推送

        /// <summary>
        /// 卡券审核事件
        /// </summary>
        /// <param name="cardEntry"></param>
        /// <param name="wechatEventHistory"></param>
        /// <param name="cardStatus"></param>
        public void CardAuditEvent(WeChatCardEntry cardEntry, MxApiWechatEventHistory wechatEventHistory, WechatCommonCardStatus cardStatus)
        {
            if (!String.Equals(cardEntry.CardStatus, cardStatus.ToString(), StringComparison.CurrentCultureIgnoreCase))
            {
                cardEntry.CardStatus = cardStatus.ToString();
            }

            try
            {
                WeChatCardEntryBLL.Instance.Update(cardEntry);

                switch (cardEntry.CardType.ToUpper())
                {
                    case "MEMBER_CARD":
                        var memberCardProccess = new MemberCardProccess(wechatEventHistory.CardId, _wechatInfo);
                        memberCardProccess.CardAuditEvent(_wechatEventHistory, cardStatus);
                        break;
                    case "DISCOUNT":
                    case "CASH":
                    case "GIFT":
                    case "GENERAL_COUPON":
                    case "GROUPON":
                        //TODO: 普通卡券逻辑
                        break;
                    default:
                        _fileLog.Info("WechatCardEntry | CardAuditEvent | Cannot support the card type now - " +
                                      cardEntry.CardType);
                        break;
                }
            }
            catch (Exception ex)
            {
                _fileLog.Error("WechatCardEntry | CardAuditEvent | Cannot audit card info - " + ex.Message +
                               ex.StackTrace);
            }
        }

        /// <summary>
        /// 领取卡券事件
        /// </summary>
        /// <param name="cardEntry"></param>
        /// <param name="wechatEventHistory"></param>
        private void CustomerGetCardEvent(WeChatCardEntry cardEntry, MxApiWechatEventHistory wechatEventHistory)
        {
            if (string.IsNullOrWhiteSpace(wechatEventHistory.UserCardCode))
            {
                _fileLog.Error("WechatCardEntry | CustomerGetCardEvent - Error CardId Value | CardId -" + wechatEventHistory.CardId);
                return;
            }

            if (string.IsNullOrWhiteSpace(wechatEventHistory.FromUserName))
            {
                _fileLog.Error("WechatCardEntry | CustomerGetCardEvent - Error FromUserName Value | CardId -" +
                               wechatEventHistory.CardId + " | UserCardCode -" + wechatEventHistory.UserCardCode);
                return;
            }

            if (
                !String.Equals(cardEntry.CardStatus, WechatCommonCardStatus.CARD_STATUS_VERIFY_OK.ToString(),
                    StringComparison.CurrentCultureIgnoreCase))
            {
                _fileLog.Error("WechatCardEntry | CustomerGetCardEvent - Error card status Value | CardId -" +
                               cardEntry.CardId + " | status -" + cardEntry.CardStatus);
                return;
            }

            try
            {
                switch (cardEntry.CardType.ToUpper())
                {
                    case "MEMBER_CARD":
                        var memberCardProccess = new MemberCardProccess(wechatEventHistory.CardId, _wechatInfo);
                        memberCardProccess.CustomerGetCardEvent(_wechatEventHistory);
                        break;
                    case "DISCOUNT":
                    case "CASH":
                    case "GIFT":
                    case "GENERAL_COUPON":
                    case "GROUPON":
                        //TODO: 普通卡券逻辑
                        break;
                    default:
                        _fileLog.Info("WechatCardEntry | CustomerGetCardEvent | Cannot support the card type now - " + cardEntry.CardType);
                        break;
                }
            }
            catch (Exception ex)
            {
                _fileLog.Error("WechatCardEntry | CustomerGetCardEvent | Cannot procces card info get-action - " +
                               ex.Message + ex.StackTrace);
            }

        }

        /// <summary>
        /// 用户删除卡券
        /// </summary>
        /// <param name="cardEntry"></param>
        /// <param name="wechatEventHistory"></param>
        private void UserDeleteCardEvent(WeChatCardEntry cardEntry, MxApiWechatEventHistory wechatEventHistory)
        {
            if (string.IsNullOrWhiteSpace(wechatEventHistory.UserCardCode))
            {
                _fileLog.Error("WechatCardEntry | UserDeleteCardEvent - Error CardId Value | CardId -" + wechatEventHistory.CardId);
                return;
            }

            if (string.IsNullOrWhiteSpace(wechatEventHistory.FromUserName))
            {
                _fileLog.Error("WechatCardEntry | UserDeleteCardEvent - Error FromUserName Value | CardId -" +
                               wechatEventHistory.CardId + " | UserCardCode -" + wechatEventHistory.UserCardCode);
                return;
            }

            try
            {
                switch (cardEntry.CardType.ToUpper())
                {
                    case "MEMBER_CARD":
                        var memberCardProccess = new MemberCardProccess(wechatEventHistory.CardId, _wechatInfo);
                        memberCardProccess.UserDeleteCardEvent(_wechatEventHistory);
                        break;
                    case "DISCOUNT":
                    case "CASH":
                    case "GIFT":
                    case "GENERAL_COUPON":
                    case "GROUPON":
                        //TODO: 普通卡券逻辑
                        break;
                    default:
                        _fileLog.Info("WechatCardEntry | UserDeleteCardEvent | Cannot support the card type now - " + cardEntry.CardType);
                        break;
                }
            }
            catch (Exception ex)
            {
                _fileLog.Error("WechatCardEntry | UserDeleteCardEvent | Cannot procces card info delete-action - " +
                               ex.Message + ex.StackTrace);
            }
        }


        #endregion






    }
}